<?php

//getting the shopping cart ID
$shoppingcart_id = $_COOKIE["shoppingcart_id"];

//using the id, get the IP address from which the cart was first set
$cart_details = mysql_query("SELECT IP, DATE FROM carts WHERE ID = '$shoppingcart_id'");

//extract the unique row
$cart_details_array = mysql_fetch_array($cart_details);

//save the data in some variables
$ip = $cart_details_array["IP"];
$date = $cart_details_array["DATE"];

//GET the client details that we presume he inserted inside the form
$client_name = $_GET["name"];
$client_address = $_GET["address"];

//insert the date along with the client details in the table oders
mysql_query("INSERT INTO orders (ID, IP, CLIENT_NAME, CLIENT_ADDRESS, DATE) VALUES ('$shoppingcart_id', '$ip', '$client_name', '$client_address', '$date')");

//the cart has now become a pleced order
//we must unset the cookie.

//set the domain for which the cookie is valid
$dom = $_SERVER['SERVER_NAME'];
if($dom[0] == "w")
	{
	//truncate the first three characters
	$domain = substr($dom, 3);
	}
else
	{
	//add point
	$domain = ".".$dom;
	}

//finally, send the cookie to the user agent
setcookie("shoppingcart_id", "", time() - 3600, "/", $domain);

//the time selected for the expiry of the cookie is already passed, which meansd that the cookie is expired

//we can also delete the data from the carts table. we do not need it any more
mysql_query("DELETE FROM carts WHERE ID = '$shoppingcart_id'");
?>